package com.invoicing.controller;

import com.invoicing.MainController;
import com.invoicing.Service.CommodityService;
import com.invoicing.entity.Commodity;
import com.invoicing.view.MenuView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

@FXMLController
public class DiscountController implements Initializable {


    private List<Commodity> commodityList = new ArrayList<Commodity>();
    private List<CheckBox> checkBoxList = new ArrayList<CheckBox>();
    DecimalFormat df = new DecimalFormat("");
    String commodity_name;
    int i = 0;
    int count = 0;

    @Autowired
    private CommodityService commodityService;

    @FXML
    private VBox vBox;

    @FXML
    private TextField discount;

    @FXML
    private Button sure;



    @FXML
    private void setDiscount(){
        if(discount.getText().length()==0){
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("注意");
            alert.setContentText("请输入折扣");
            alert.show();
        }
        else if(1<Float.parseFloat(discount.getText())){
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("注意");
            alert.setContentText("折扣不能大于1");
            alert.show();
            discount.clear();
        }
        else {
            count =0;
            i=0;
            for(i=0;i<checkBoxList.size();i++){
                if(checkBoxList.get(i).isSelected()){
                    count = count+1;
                    commodityService.updateDiscount(i+1,Float.parseFloat(discount.getText()));
                }
            }
            i=0;
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("提示");
            alert.setContentText("已经为"+count+"个商品设置折扣"+discount.getText());
            alert.show();

            discount.clear();
            vBox.getChildren().clear();
            for(int j=0;j<checkBoxList.size();j++){
                checkBoxList.get(j).setSelected(false);
                vBox.getChildren().add(checkBoxList.get(j));
            }
            flash();


        }
    }
    @FXML
    private void flash(){
        commodityList = commodityService.listAll();
        vBox.getChildren().clear();
        checkBoxList.clear();
        int k=0,temp =0;
        for(int i=0;i<commodityList.size();i++){
            char[] name = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
            CheckBox checkBox = new CheckBox();
            k=0;
            temp = 0;
            for(int j=0;j<commodityList.get(i).getName().length();j++){
                if(commodityList.get(i).getName().charAt(j)>255){
                    name[j] = commodityList.get(i).getName().charAt(j);
                    k++;
                }
            }
            temp = k;
            k=25-2*k;
            for(int j=0;j<String.valueOf(commodityList.get(i).getPrice()).length();j++){
                name[k] = String.valueOf(commodityList.get(i).getPrice()).charAt(j);
                k++;
            }
            k=53-2*temp;
            for(int j=0;j<String.valueOf(commodityList.get(i).getDiscount()).length();j++){
                name[k] = String.valueOf(commodityList.get(i).getDiscount()).charAt(j);
                k++;
            }
            commodity_name = String.valueOf(name);
            checkBox.setText(commodity_name);
            checkBox.setStyle("-fx-padding:2px,0px,2px,0px");
            checkBoxList.add(checkBox);
            vBox.getChildren().add(checkBox);
        }

    }
    @FXML
    private void setBack(){
        MainController.showView(MenuView.class);
        discount.clear();
        for(int j=0;j<checkBoxList.size();j++){
            checkBoxList.get(j).setSelected(false);
        }

    }

    public void initialize(URL location, ResourceBundle resources) {

        commodityList = commodityService.listAll();
        int k=0,temp =0;
        for(int i=0;i<commodityList.size();i++){
            char[] name = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
            CheckBox checkBox = new CheckBox();
            checkBox.setStyle("-fx-padding:2px,0px,2px,0px");
            k=0;
            temp = 0;
            for(int j=0;j<commodityList.get(i).getName().length();j++){
                if(commodityList.get(i).getName().charAt(j)>255){
                    name[j] = commodityList.get(i).getName().charAt(j);
                    k++;
                }
            }
            temp = k;
            k=25-2*k;
            for(int j=0;j<String.valueOf(commodityList.get(i).getPrice()).length();j++){
                    name[k] = String.valueOf(commodityList.get(i).getPrice()).charAt(j);
                    k++;
            }
            k=53-2*temp;
            for(int j=0;j<String.valueOf(commodityList.get(i).getDiscount()).length();j++){
                name[k] = String.valueOf(commodityList.get(i).getDiscount()).charAt(j);
                k++;
            }
            commodity_name = String.valueOf(name);
            checkBox.setText(commodity_name);
            checkBoxList.add(checkBox);
            vBox.getChildren().add(checkBox);
        }
    }
}
